﻿package com.gamemeal.particle.steering {
	import com.gamemeal.geom.VelocityPoint3;
	/**
	 * @author Colin
	 */
	public class Spring implements ISteering{
		private var k:Number;
		private var inertia:Number=0.9;
		private var imp:ISteering;
		
		public function Spring(springConstant:Number=0.1,inertia:Number=0.9,enable3D:Boolean=true){
			this.k = springConstant;
			this.inertia = inertia;
			
			imp = (enable3D)?new impSpring3D():new impSpring2D();

		}

		public function apply(p:VelocityPoint3,tx:Number,ty:Number,tz:Number):void{
			imp.apply(p,tx,ty,tz);
		}
	}
}

import com.gamemeal.geom.VelocityPoint3;

class impSpring2D implements ISteering{
	private var k:Number;
	private var inertia:Number=0.9;
	
	public function impSpring2D(springConstant:Number,inertia:Number){
		this.k = springConstant;
		this.inertia = inertia;
	}
	
	public function apply(p:VelocityPoint3,tx:Number,ty:Number,tz:Number=null):void{
		tz;
		// We calculate the distance to the mouse
		var x:Number = -p.x + tx ;
		var y:Number = -p.y + ty ;
		
		//We calculate the amount by which the mass will to move
		p.vx = p.vx * inertia + x*k ;
		p.vy = p.vy * inertia + y*k ;
	}
}

class impSpring3D implements ISteering{
	private var k:Number;
	private var inertia:Number=0.9;
	
	public function impSpring3D(springConstant:Number,inertia:Number){
		this.k = springConstant;
		this.inertia = inertia;
	}
	
	public function apply(p:VelocityPoint3,tx:Number,ty:Number,tz:Number=null):void{
		// We calculate the distance to the mouse
		var x:Number = -p.x + tx ;
		var y:Number = -p.y + ty ;
		var z:Number = -p.z + tz ;
		
		//We calculate the amount by which the mass will to move
		p.vx = p.vx * inertia + x*k ;
		p.vy = p.vy * inertia + y*k ;
		p.vz = p.vz * inertia + z*k ;
	}
}

